package vivek.project.pocketmoney.mutualfund.model.utils
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import mx.collections.ArrayCollection;
	import vivek.project.pocketmoney.common.utils.DateUtils;
	import vivek.project.pocketmoney.mutualfund.model.MutualFundDataCache;

	public class MutualFundPriceUpdate extends EventDispatcher
	{
		public var bytesLoaded:int=0;
		public var bytesTotal:int=0;
		private var i:int=0;
		private var schemeControl:ArrayCollection=MutualFundDataCache.schemeControl;
		private var threads:Array=new Array(schemeControl.length);

		public function updateStockPrices():void
		{
			
			var mfObj:Object=new Object();
			{
				var endDate:Date=new Date();
				for each (mfObj in schemeControl)
				{
					var _startDate:Date=mfObj.sm_lastupdated;
					//while(_startDate <= endDate)
					//if (mfObj.sm_schemeid == 640)
					{
						var mfpuw:GoogleAppMutualFundPriceUpdateWorker=new GoogleAppMutualFundPriceUpdateWorker();
						mfpuw.addEventListener("UpdateCompletedSuccessFully", updateCount);
						mfpuw.addEventListener("UpdateCompletedWithError", updateCount);
						threads.push(mfpuw);
						mfpuw.getNAVData(mfObj, _startDate, endDate);
						//			_startDate=DateUtils.dateAdd("date",1,_startDate);
					}
				}
			}
		}

		private function updateCount(event:Event):void
		{
			i=i + 1;
			//trace("value " + i + "Total: "+schemeControl.length*2);
			dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, i, schemeControl.length));
		}
	}
}